package com.shuoyue.ycgk.ui.main.today_count;

import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.TodayCourseInfo;
import com.shuoyue.ycgk.entity.TodayQuestionInfo;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;
import com.shuoyue.ycgk.utils.XDateUtils;

import io.reactivex.Observable;

public interface TodayLearnContract {
    interface View extends BaseView {

        void setTodayQuestionInfo(TodayQuestionInfo todayQuestionInfo);

        void setTodayCourseInfo(TodayCourseInfo todayCourseInfo);

    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void getTodayInfo(int modelId, int page, int type) {
            if (type == 1) {

                apply(model.getTodayQuestionInfo(modelId, page))
                        .subscribe(new ApiSubscriber<Optional<TodayQuestionInfo>>(mView, this) {
                            @Override
                            public void onNext(Optional<TodayQuestionInfo> option) {
                                super.onNext(option);
                                mView.setTodayQuestionInfo(option.getIncludeNull());
                            }
                        });
            } else {
                apply(model.getTodayCourseInfo(modelId, page))
                        .subscribe(new ApiSubscriber<Optional<TodayCourseInfo>>(mView, this) {
                            @Override
                            public void onNext(Optional<TodayCourseInfo> option) {
                                super.onNext(option);
                                mView.setTodayCourseInfo(option.getIncludeNull());
                            }
                        });
            }
        }
    }

    class Model {
        Observable<BaseResult<TodayQuestionInfo>> getTodayQuestionInfo(int moduleId,
                                                                       int n) {
            return RetrofitClient.getInstance().getApi().getTodayQuestionInfo(moduleId, n, 1, XDateUtils.format(System.currentTimeMillis(), "yyyy-MM-dd"));
        }

        Observable<BaseResult<TodayCourseInfo>> getTodayCourseInfo(int moduleId,
                                                                   int n) {
            return RetrofitClient.getInstance().getApi().getTodayCourseInfo(moduleId, n, 2, XDateUtils.format(System.currentTimeMillis(), "yyyy-MM-dd"));
        }
    }
}
